#!/bin/bash

# Mount an NFS export for remote log storage.

# Copyright (C) 2003-2006 IBM
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

# @author Sarunya Jimenez (sjimen@us.ibm.com)
# Reworked by Darrick Wong <djwong@us.ibm.com>
#  - If unable to connect to NFS_SERVER => save log files in local machine
#  - Otherwise,
#    1. log files produced from ./pounder will be in
#       NFS_LOGSERVER (e.g. 10.0.0.211)
#       |--- /pounder
#            |---   /$HOSTNAME (e.g. testbox)
#                   |--- KERNEL_VERSION-ARCH (e.g. 2.6.14-i686)
#    2. Run "$./pounder -u" to umount nfs log server
#       Otherwise, by default, when ./pounder completed, nfs remains
#       mounted to the local machine.
# ASSUMPTION  : Already imported global variables from "libpounder.sh"

if [ -z "$NFS_LOGSERVER" -o -z "$NFS_LOGDIR" ]; then
	echo "NFS log server not configured."
	exit 0
fi

# Path construction:

#nfsserv:/crash/pounder-logs/testbox/2.6.14-i686/somedate/
#$NFS_LOGSERVER:$NFS_LOGDIR/$NFS_LOGLOCAL/$DATE

#/home/pounder/log
#$POUNDER_LOGLOCAL

#/home/pounder/log/somedate
#$POUNDER_LOGDIR or $POUNDER_LOGLOCAL/$DATE

# Are we already mounted?
IS_MOUNTED=`grep "$POUNDER_LOGLOCAL " /proc/mounts | wc -l`
if [ $IS_MOUNTED -eq 1 ]; then
	echo "Log directory already mounted on $POUNDER_LOGLOCAL"
	exit 0
fi

# Create local directory for mounting
mkdir -p "$POUNDER_LOGLOCAL/"
if [ ! -d "$POUNDER_LOGLOCAL/" ]; then
	echo "Cannot create $POUNDER_LOGLOCAL/."
	exit 1
fi

# Mount NFS log server's top-level log dir
mount "$NFS_LOGSERVER:$NFS_LOGDIR/" "$POUNDER_LOGLOCAL/" -t nfs -o tcp
RESULT=$?

if [ $RESULT -gt 0 ]; then
	echo "Mounting $NFS_LOGSERVER:$NFS_LOGDIR/ on $POUNDER_LOGLOCAL/ failed; logs will be local."
	exit 2;
fi

# Create a directory for this run's log files
mkdir -p "$POUNDER_LOGLOCAL/$NFS_LOGLOCAL/"
if [ ! -d "$POUNDER_LOGLOCAL/$NFS_LOGLOCAL/" ]; then
	echo "Cannot create local log dir on log server $POUNDER_LOGLOCAL/$NFS_LOGLOCAL/; logs will be local."
	umount "$POUNDER_LOGLOCAL/"
	exit 3
fi

# Now remount the real log dir on our local machine.
umount "$POUNDER_LOGLOCAL/"
mount  "$NFS_LOGSERVER:$NFS_LOGDIR/$NFS_LOGLOCAL/" "$POUNDER_LOGLOCAL/" -t nfs -o tcp
RESULT=$?

if [ $RESULT -gt 0 ]; then
	echo "Mounting $NFS_LOGSERVER:$NFS_LOGDIR/$NFS_LOGLOCAL on $POUNDER_LOGLOCAL/ failed; logs will be local."
	exit 4;
fi

# Once we return to pounder, it'll create $POUNDER_LOGDIR.

exit 0
